<template>
	<view class="container-gray">
		<view>
			<view class="fixed-box">
				<view
					class="flex-box"
					style="border-top: 1px solid #EDEDED;padding: 0 30upx; height:100upx; box-sizing: border-box; display;justify-content: space-between;font-size: 14px;font-weight: 500;"
				>
					<view @click="allCourse">
						全部课程
						<image style="width: 23upx;height: 15upx;margin-left: 6px;" src="../../static/img/xxjt.png" />
					</view>
					<view v-if="showIng" @click="newest">
						{{ stateName }}
						<image style="width: 23upx;height: 15upx;margin-left: 6px;" src="../../static/img/bluesx.png" />
					</view>
					<view v-else @click="newest">
						{{ stateName }}
						<image style="width: 23upx;height: 15upx;margin-left: 6px;" src="../../static/img/xxjt.png" />
					</view>
				</view>
				<view v-if="showIng" @click="isShowIng" style="background: rgba(0,0,0,0.5);position: fixed;z-index: 1001;width: 100%;height: 100%;display: flex;">
					<view style="text-align: center;width: 100%;height: 90px;background: #FFFFFF; font-size: 14px; font-weight: 400;">
						<view @click="coursestate('最新', 2)" style="padding: 15upx 0; line-height: 30px;">最新</view>
						<view @click="coursestate('最热', 1)" style="padding: 15upx 0;">最热</view>
					</view>
				</view>
			</view>
			<view>
				<view style="margin-top: 110upx;padding: 10upx 20upx;">
					<view v-if="dataList.length">
						<view v-for="(item, index) in dataList" :key="index">
							<view class="flex-box course-info-box" @click="goStudy(item)">
								<image class="info-img" :src="item.imgUrl" options mode="aspectFill" />
								<view style="margin-left: 10upx;flex:1;">
									<view class="info-title" style="height: 120upx;">{{ item.title }}</view>
									<view style="font-size: 20upx;color: #6A6A6A;" v-if="item.status == 0">未学习</view>
									<view style="font-size: 20upx;color: #6A6A6A;" v-if="item.status == 1">未完成</view>
									<view style="font-size: 20upx;color: #6A6A6A;" v-if="item.status == 2">已完成</view>
								</view>
							</view>
						</view>
						<uni-load-more :status="loadingType" :contentText="contentText"></uni-load-more>
					</view>
					<view v-else class="container-tips">暂无相关内容</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { post_md5, request_integral } from '../../common/request';
import { setNavTitle } from '../../common/common';
import xflSelect from '@/components/xfl-select/xfl-select.vue';
export default {
	data() {
		return {
			view: null,
			list: [
				//要展示的数据
				'最新',
				'最热'
			],
			showIng: false,
			title: '',
			logo: '',
			courseId: null,
			stateName: '最新',
			dataList: [],
			page: 1,
			cateId: '',
			idx: 2,
			cateList: [],
			refreshText: '下拉可以刷新',
			loadingType: 'more', //定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
			contentText: {
				contentdown: '上拉加载更多',
				contentrefresh: '正在加载...',
				contentnomore: '没有更多数据了'
			}
		};
	},
	components: {
		xflSelect
	},
	onLoad(options) {
		this.cateId = options.id;
		this.getCourseList();
	},
	onShow() {
		this.getTitleLogo();
		if (this.courseId != null) {
			this.getcoursetype();
		}
		this.loadingType = 'more';
	},
	// onHide() {
	// 	if (this.view !== null && this.view !== undefined) {
	// 		this.view.close();
	// 		this.view = null;
	// 	}
	// },
	// onUnload() {
	// 	if (this.view !== null && this.view !== undefined) {
	// 		this.view.close();
	// 		this.view = null;
	// 	}
	// },
	onPullDownRefresh: function() {
		this.getTitleLogo();
		//下拉刷新的时候请求一次数据
		this.page = 1;
		uni.setStorageSync('couresList', '[]');
		this.dataList = [];
		this.loadingType = 'more';
		this.getCourseList();
	},
	onReachBottom: function() {
		this.page++;
		this.getCourseList(this.idx);
	},
	onNavigationBarButtonTap(Object) {
		this.goSearch();
	},
	methods: {
		getTitleLogo() {
			// if (this.view !== null && this.view !== undefined) {
			// 	this.view.close();
			// 	this.view = null
			// }
			// #ifdef APP-PLUS
			post_md5('/sys/tenantsysconfig/appinfo').then(res => {
				let data = JSON.parse(res.data);
				setNavTitle(data.traintitle);
				// this.view = setNavLogo(data.logoUrl)
				// this.view.show()
			});
			// #endif
		},
		// 点击状态
		coursestate(stateName, val) {
			uni.setStorageSync('couresList', '[]');
			this.dataList = [];
			this.page = 1;
			this.stateName = stateName;
			this.idx = val;
			this.getCourseList(this.idx);
		},
		getcoursetype() {
			const params = {
				courseId: this.courseId,
				userId: uni.getStorageSync('userId')
			};
			post_md5('/train-app/courseapp/coursestatus', params).then(res => {
				if (res.data == 2) request_integral('course', this.courseId);
				this.dataList.forEach(x => {
					if (x.id == this.courseId) {
						x.status = res.data;
					}
				});
			});
		},
		isShowIng() {
			this.showIng = false;
		},
		// 点击最新
		newest() {
			this.showIng = true;
		},
		getCourseList(idx) {
			this.loadingType = 'loading';
			uni.showNavigationBarLoading(); //显示加载动画idx
			const params = {
				page: this.page ? this.page : 1,
				userId: uni.getStorageSync('userId'),
				token: uni.getStorageSync('token'),
				orderType: idx ? idx : 2
			};
			if (this.cateId) {
				params.cateId = this.cateId;
			}
			post_md5('/train-app/courseapp/getcourselist', params).then(res => {
				uni.stopPullDownRefresh();
				if (res.data.list) {
					for (let i in res.data.list) {
						this.dataList.push(res.data.list[i]);
					}
					uni.setStorageSync('couresList', JSON.stringify(this.dataList));
					this.loadingType = 'more';
				} else {
					this.loadingType = 'noMore';
				}
				if (this.dataList.length == res.data.totalCount) {
					this.loadingType = 'noMore';
				}

				uni.hideNavigationBarLoading(); //关闭加载动画
			});
		},
		goStudy(item) {
			if (item.permission == 1) {
				uni.showToast({
					title: '暂无权限',
					icon: 'none'
				});
				return;
			} else {
				this.courseId = item.id;
				uni.navigateTo({
					url: 'detail' + `?id=${item.id}&imgUrl=${item.imgUrl}&status=${item.status}`
				});
			}
		},
		allCourse() {
			uni.navigateTo({
				url: '/pages/allcourse/allcourse'
			});
		},
		goSearch() {
			uni.navigateTo({
				url: '/pages/search/search'
			});
		}
	}
};
</script>

<style>
.img {
	width: 28upx;
	height: 20upx;
	margin-top: 8upx;
	margin-left: 8upx;
}

.container-tips {
	min-height: 50vh;
	display: flex;
	justify-content: center;
	align-items: center;
	font-size: 32upx;
	color: #fff;
}

.status_bar {
	height: var(--status-bar-height);
	width: 100%;
	background-color: #ffffff;
}

.top_view {
	height: var(--status-bar-height);
	width: 100%;
	background-color: #ffffff;
	position: fixed;
	top: 0;
	z-index: 999;
}

.fixed-box {
	width: 100%;
	position: fixed;
	/* top: var(--status-bar-height); */
	background: #ffffff;
	z-index: 10;
}

.tab-bar {
	height: 80upx;
	line-height: 80upx;
	color: #000000;
	font-size: 32upx;
	width: 100%;
	background: #ffffff;
}

.tab {
	width: 750upx;
	overflow: hidden;
	/* position: fixed; */
}

.train-info-box {
	width: 650upx;
	border-radius: 40upx;
	box-shadow: 10upx 6upx 30upx #e2e8ec;
	margin-top: 30upx;
	margin-left: 30upx;
	padding: 20upx;
	display: flex;
	position: relative;
}

.course-info-box {
	flex: 1;
	padding: 20upx;
	margin-bottom: 20upx;
	background: #ffffff;
	border-radius: 20upx;
}

.info-img {
	width: 350upx;
	height: 180upx;
	border-radius: 20upx;
}

.top-type {
	width: 100%;
}

.info-title {
	flex: 1;
	font-size: 28upx;
	font-weight: bold;
	color: #333333;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 3;
	line-height: 40upx;
}

.info-brief {
	font-size: 22upx;
	color: #666666;
	line-height: 30upx;
	height: 60upx;
	margin-top: 10upx;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
}

.info-tips {
	font-size: 24upx;
	font-weight: 400;
	color: #848484;
	/* margin-top: 30upx; */
	display: flex;
	align-items: center;
}
</style>
